package 学生成绩管理器;

import java.util.Scanner;

public class Manager {
    private int numberOfStudent;
    private Student[] students;

    public void setNumberOfStudent(Scanner scanner) {
        System.out.print("请输入学生人数：");
        while (true) {
            if (scanner.hasNextInt()) {
                numberOfStudent = scanner.nextInt();
                if (numberOfStudent <= 0) {
                    scanner.nextLine();
                    System.out.println("输入错误！请输入大于0的整数！");
                    System.out.print("请输入学生人数：");
                    continue;
                }
                break;
            } else {
                scanner.nextLine();
                System.out.println("输入错误！请输入大于0的整数！");
                System.out.print("请输入学生人数：");
            }
        }
        scanner.nextLine();
        createClass();
    }

    private void createClass() {
        students = new Student[numberOfStudent];
        for (int i = 0; i < students.length; i++) {
            students[i] = new Student();
        }
    }

    public void inputNameAndGrade(Scanner scanner) {
        for (int i = 0; i < students.length; i++) {
            System.out.print("请输入第" + (i + 1) + "个学生的姓名：");
            students[i].setName(scanner.nextLine());
            System.out.print("请输入" + students[i].getName() + "的成绩：");
            while (true) {
                if (scanner.hasNextInt()) {
                    int grade = scanner.nextInt();
                    if (grade < 0 || grade > 100) {
                        System.out.println("请输入大于等于0且不超过100的整数！");
                        System.out.print("请输入" + students[i].getName() + "的成绩：");
                    } else {
                        students[i].setGrade(grade);
                        break;
                    }
                } else {
                    System.out.println("请输入大于等于0且不超过100的整数！");
                    System.out.print("请输入" + students[i].getName() + "的成绩：");
                }
            }
            scanner.nextLine();
        }
        System.out.println();
    }

    private String printFormat(Student student) {
        return String.format("%s - %d", student.getName(), student.getGrade());
    }

    public void printInformationOfStudent() {
        System.out.println("所有学生信息：");
        for (Student student : students) {
            System.out.println(printFormat(student));
        }
        System.out.println();
    }

    public void calculateAndPrintAverageScore() {
        int sum = 0;
        for (Student student : students) {
            sum += student.getGrade();
        }
        System.out.printf("平均分：%.1f\n", ((double)sum / numberOfStudent));
    }

    public void getAndPrintHighestScore() {
        int highestScore = students[0].getGrade();
        Student bestStudent = students[0];
        for (int i = 1; i < students.length; i++) {
            if (students[i].getGrade() > highestScore) {
                highestScore = students[i].getGrade();
                bestStudent = students[i];
            }
        }
        System.out.println("最高分：" + printFormat(bestStudent));
    }

    public void getAndPrintFailingStudents() {
        boolean hasFailed = false;
        System.out.println("不及格学生：");
        for (Student student : students) {
            if (student.getGrade() < 60) {
                hasFailed = true;
                System.out.println(printFormat(student));
            }
        }
        if (!hasFailed) {
            System.out.println("无");
        }
    }
}
